from connect.mqtt_client_singleton import MqttClientSingleton
from flask_app import app

if __name__ == '__main__':
    broker_address = "119.91.228.85"
    broker_port = 1883
    client_id = "srt_app_server"
    username = 'srt_app_server'
    password = 'srt_app_server'
    topic = '/sys/pv/temputrate'

    # 创建或获取单例实例
    mqtt_client_singleton = MqttClientSingleton(broker_address, broker_port, client_id, username, password)

    # 获取 mqtt_client 对象
    mqtt_client = mqtt_client_singleton.mqtt_client

    # 准备要发布的数据 (test)
    data_to_publish = {"temperature": 38, "voltage": 0.4}
    # 发布数据到指定主题
    mqtt_client_singleton.publish_data(topic, data_to_publish)

    # 循环保持连接
    mqtt_client.loop_forever()
    # 启动 Flask 应用
    app.run(debug=True)
